<template>
  <div class="view" v-if="nearbyData">
      <div class="view_one">
          <span v-for="(itme,index) in viewthetraffic" 
            :key="index" 
            @click="spread_traffic_attractions(index)" 
            :class="{acit:type==index}"
          >{{itme.type}}</span>
      </div>
      <div class="view_neighbouring">
          <div 
              class="view_neighbouring_jindian" 
              v-for="(itme,index) in nearbyData" 
              :key="index" 
              @mouseenter="to_display(index)"
            >
              <div class="name">
                {{itme.name}}
              </div>
              <div class="distance">
                 {{siteDistance[index]}} 米
              </div>
          </div>
      </div>

  </div>
</template>

<script>
export default {
    props:['nearbyData','hotel_detail'],
    data(){
        return {
            viewthetraffic:[{type:'景点'},{type:'交通'}],
            type:0,
            siteDistance:[],  // 距离
        }
    },
    created () {
        this.ForDistance()
    },
    watch : {
        nearbyData:{
            deep:true,
            handler(newV){
                this.ForDistance()
            }
        } 
    },
    methods: {
        //  获取距离
        ForDistance () {
            let latitude = (this.hotel_detail.location.latitude).toFixed(6) 
            let longitude = (this.hotel_detail.location.longitude).toFixed(6)
            this.siteDistance = []
            this.nearbyData.forEach( async (v)=>{
                if(v.name === this.hotel_detail.name){
                    return
                }
                const res = await this.$axios.get('https://restapi.amap.com/v3/direction/walking?parameters',{
                    params:{
                        key: '34723cdff8d29820decbe8b8284ecfbb',
                        origin: longitude +','+ latitude,
                        destination: v.location,
                    }
                })
                this.siteDistance.push(res.data.route.paths[0].distance)
            })
        },
        to_display(index) {
            this.$emit("send_center_data",index)
        },
        // 景点交通切换
        spread_traffic_attractions(index){
            this.type = index
            this.$emit('spread_traffic_attractions',index)
        }   
    }
}
</script>

<style lang="less" scoped>
.view {
    width: 100%;
    height: 100%;
    .view_one {
        width: 100%;
        height: 42px;
        line-height: 42px;
        border-bottom: 2px solid #dddddd;
        span {
            padding-right: 30px;
            cursor: pointer;
            font-size: 16px;
            color: #666666;
            &.acit {
                color: #409eff;
            }
        }
    }
    .view_neighbouring {
        height: 300px;
        width: 100%;
        padding: 10px 0 0 10px;
        margin-top: 10px;
        overflow: auto;
        cursor: pointer;
        .view_neighbouring_jindian {
            display: flex;
            justify-content: space-between;
            height: 36px;
            line-height: 36px;
            color: #666666;
            font-size: 14px;
            .distance {
                padding-right: 20px;
            }
        }
    }
}
</style>